<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="注册类型(1:手机号,2:用户名3:微信号注册)" prop="regType">
        <el-select clearable v-model="queryParams.regType" placeholder="请选择注册类型(1:手机号,2:用户名3:微信号注册)" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户唯一标识" prop="uuid">
        <el-input
          v-model="queryParams.uuid"
          placeholder="请输入用户唯一标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推荐人ID" prop="inviteCode">
        <el-input
          v-model="queryParams.inviteCode"
          placeholder="请输入推荐人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="sv_1_pc, sv_1_ios, sv_1_android" prop="appid">
        <el-input
          v-model="queryParams.appid"
          placeholder="请输入sv_1_pc, sv_1_ios, sv_1_android"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信OPENID" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入微信OPENID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盐" prop="salt">
        <el-input
          v-model="queryParams.salt"
          placeholder="请输入盐"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入用户真实姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别(-1,保密，0女，1男)" prop="sex">
        <el-select clearable v-model="queryParams.sex" placeholder="请选择性别(-1,保密，0女，1男)" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="年龄(-1,保密)" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄(-1,保密)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idcard">
        <el-input
          v-model="queryParams.idcard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图像" prop="avatar">
        <el-input
          v-model="queryParams.avatar"
          placeholder="请输入图像"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户签名" prop="signature">
        <el-input
          v-model="queryParams.signature"
          placeholder="请输入用户签名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-input
          v-model="queryParams.birthday"
          placeholder="请输入生日"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label=" 0禁用，1解禁" prop="status">
        <el-select clearable v-model="queryParams.status" placeholder="请选择 0禁用，1解禁" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="分享码" prop="shareCode">
        <el-input
          v-model="queryParams.shareCode"
          placeholder="请输入分享码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户积分" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入用户积分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户的默认收货地址ID" prop="addressId">
        <el-input
          v-model="queryParams.addressId"
          placeholder="请输入用户的默认收货地址ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册IP" prop="regIp">
        <el-input
          v-model="queryParams.regIp"
          placeholder="请输入注册IP"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:userInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:userInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:userInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:userInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="userId" v-if="false"/>
      <el-table-column label="注册类型(1:手机号,2:用户名3:微信号注册)" align="center" prop="regType" />
      <el-table-column label="用户唯一标识" align="center" prop="uuid" />
      <el-table-column label="推荐人ID" align="center" prop="inviteCode" />
      <el-table-column label="sv_1_pc, sv_1_ios, sv_1_android" align="center" prop="appid" />
      <el-table-column label="微信OPENID" align="center" prop="openid" />
      <el-table-column label="设备编号" align="center" prop="deviceId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="盐" align="center" prop="salt" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="用户真实姓名" align="center" prop="realName" />
      <el-table-column label="性别(-1,保密，0女，1男)" align="center" prop="sex" />
      <el-table-column label="年龄(-1,保密)" align="center" prop="age" />
      <el-table-column label="身份证号" align="center" prop="idcard" />
      <el-table-column label="图像" align="center" prop="avatar" />
      <el-table-column label="用户签名" align="center" prop="signature" />
      <el-table-column label="生日" align="center" prop="birthday" />
      <el-table-column label=" 0禁用，1解禁" align="center" prop="status" />
      <el-table-column label="分享码" align="center" prop="shareCode" />
      <el-table-column label="用户积分" align="center" prop="score" />
      <el-table-column label="用户的默认收货地址ID" align="center" prop="addressId" />
      <el-table-column label="注册IP" align="center" prop="regIp" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:userInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:userInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="注册类型(1:手机号,2:用户名3:微信号注册)" prop="regType">
          <el-select clearable v-model="form.regType" placeholder="请选择注册类型(1:手机号,2:用户名3:微信号注册)">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户唯一标识" prop="uuid">
          <el-input v-model="form.uuid" placeholder="请输入用户唯一标识" />
        </el-form-item>
        <el-form-item label="推荐人ID" prop="inviteCode">
          <el-input v-model="form.inviteCode" placeholder="请输入推荐人ID" />
        </el-form-item>
        <el-form-item label="sv_1_pc, sv_1_ios, sv_1_android" prop="appid">
          <el-input v-model="form.appid" placeholder="请输入sv_1_pc, sv_1_ios, sv_1_android" />
        </el-form-item>
        <el-form-item label="微信OPENID" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入微信OPENID" />
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="盐" prop="salt">
          <el-input v-model="form.salt" placeholder="请输入盐" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="用户真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入用户真实姓名" />
        </el-form-item>
        <el-form-item label="性别(-1,保密，0女，1男)" prop="sex">
          <el-select clearable v-model="form.sex" placeholder="请选择性别(-1,保密，0女，1男)">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="年龄(-1,保密)" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄(-1,保密)" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idcard">
          <el-input v-model="form.idcard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="图像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入图像" />
        </el-form-item>
        <el-form-item label="用户签名" prop="signature">
          <el-input v-model="form.signature" placeholder="请输入用户签名" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-input v-model="form.birthday" placeholder="请输入生日" />
        </el-form-item>
        <el-form-item label=" 0禁用，1解禁">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分享码" prop="shareCode">
          <el-input v-model="form.shareCode" placeholder="请输入分享码" />
        </el-form-item>
        <el-form-item label="用户积分" prop="score">
          <el-input v-model="form.score" placeholder="请输入用户积分" />
        </el-form-item>
        <el-form-item label="用户的默认收货地址ID" prop="addressId">
          <el-input v-model="form.addressId" placeholder="请输入用户的默认收货地址ID" />
        </el-form-item>
        <el-form-item label="0默认未删除,1删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入0默认未删除,1删除" />
        </el-form-item>
        <el-form-item label="注册IP" prop="regIp">
          <el-input v-model="form.regIp" placeholder="请输入注册IP" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserInfo, getUserInfo, delUserInfo, addUserInfo, updateUserInfo, exportUserInfo } from "@/api/business/userInfo";

export default {
  name: "UserInfo",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        regType: undefined,
        uuid: undefined,
        inviteCode: undefined,
        appid: undefined,
        openid: undefined,
        deviceId: undefined,
        userName: undefined,
        password: undefined,
        salt: undefined,
        nickName: undefined,
        mobile: undefined,
        realName: undefined,
        sex: undefined,
        age: undefined,
        idcard: undefined,
        avatar: undefined,
        signature: undefined,
        birthday: undefined,
        status: undefined,
        shareCode: undefined,
        score: undefined,
        addressId: undefined,
        regIp: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        uuid: [
          { required: true, message: "用户唯一标识不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: " 0禁用，1解禁不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "注册时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUserInfo(this.queryParams).then(response => {
        this.userInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        regType: undefined,
        uuid: undefined,
        inviteCode: undefined,
        appid: undefined,
        openid: undefined,
        deviceId: undefined,
        userName: undefined,
        password: undefined,
        salt: undefined,
        nickName: undefined,
        mobile: undefined,
        realName: undefined,
        sex: undefined,
        age: undefined,
        idcard: undefined,
        avatar: undefined,
        signature: undefined,
        birthday: undefined,
        status: 0,
        shareCode: undefined,
        score: undefined,
        addressId: undefined,
        delFlag: undefined,
        createTime: undefined,
        updateTime: undefined,
        regIp: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getUserInfo(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUserInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$confirm('是否确认删除用户编号为"' + userIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUserInfo(userIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUserInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
